package com.das.metrocloud.ssoserver.aop;

import com.das.metrocloud.ssoserver.util.URLExcluder;

import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
*  配置 SpringMVC
*/
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {


    private static Logger log = LoggerFactory.getLogger(WebMVCConfig.class);


    /**
    *  自动注入
    */
    @Autowired
    private ServiceAuthorizationInterceptor serviceAuthorizationInterceptor;


    public WebMVCConfig() {}


    /**
    *  添加 认证 拦截去
    **/
    public void addInterceptors(InterceptorRegistry registry) {


        log.info("Added Authentication Interceptor Successfully.");

        // 拦截器 注册器
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(this.serviceAuthorizationInterceptor);


        // 添加拦截规则
        interceptorRegistration.addPathPatterns(new String[]{"/**"});


        //  URL:  排除器
        Iterator var3 = URLExcluder.urlList.iterator();


        while(var3.hasNext()) {

            String url = (String)var3.next();

            // 设置拦截的 排除 RUL 规则
            interceptorRegistration.excludePathPatterns(new String[]{url});

        }

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
